<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUISApp
 *
 * @author jianfeng
 */
class BUIServiceApp extends BUINode {
    
    const View_ServiceAppTopBar = 'serviceAppTopBar';

    public $layoutView = null;
    public $views = null;

    public function __construct($node = NULL) {
        parent::__construct($node);
        $this->sApp = $node;
        $uiPath = $node->uiPath;

        if ($this->viewPath == NULL)
            $this->viewPath = $uiPath . ".themes." . Blyn::app()->getAppUI()->getTheme() . '.views';

        if ($this->formPath == NULL)
            $this->formPath = $uiPath . ".themes." . Blyn::app()->getAppUI()->getTheme() . '.forms';
    }

    public function getName() {
        return $this->sApp->getName();
    }

    public function getViews() {

        /*

          $currentNode = Blyn::app()->getCurrentNode();

          if ($currentNode instanceof BUIOperation) {
          $oppUiNode = Blyn::app()->getActiveOperation()->getUIManager();
          $this->views['childOppLayout'] = array('view' => $oppUiNode->getLayoutView(), 'data' => array('views' => $oppUiNode->getViews()));
          }

          if ($this->currentScreenFlow != null) {
          $this->views[$this->currentScreenFlow->getName()] =
          array('view' => $this->currentScreenFlow->getLayoutView(),
          'data' => array('views' => $this->currentScreenFlow->getViews()));
          }

          if ($this->currentScreen != null) {
          $this->views[$screen->getName()] = array('view' => $screen->getLayoutView(), 'data' => array('views' => $screen->getViews()));
          }
         * 
         */

        return $this->views;
    }

    public function getLayoutView() {
        if ($this->layoutView == NULL)
            $this->layoutView = 'serviceAppLayout2';

        return $this->layoutView;
    }

    //put your code here

    public function render($data = null) {
        
        $this->sApp->beforeRenderServiceApplication($data);
        parent::render();

        if ($this->views == NULL)
            $this->views = array();

        //$this->views['topBar'] = array('view' => 'serviceAppTopBar', 'uiObj' => $this);
        // $aService = $this->getActiveService();
        // $serviceUi = $aService->getUIManager();

        Blyn::app()->getActiveService()->getUIManager()->addRenderView('ServiceTopBar', 'serviceTopBar');
        Blyn::app()->getActiveService()->getUIManager()->addRenderView('ServiceAppLayout', $this->getLayoutView(), array('uiObj' => $this));
        $this->addRenderView('serviceAppTopBar', 'serviceAppTopBar');
        
        $this->sApp->afterRenderServiceApplication($data);



        //$this->renderView($this->layoutView, $this->getViews());
        //add screen to render
        /*
          $sf = $this->getCurrentScreenFlow() == NULL ? $this->getStartScreenFlow() : $this->getCurrentScreenFlow();
          $screen = $this->getCurrentScreen() == NULL ? $this->getStartScreen() : $this->getCurrentScreen();

          if ($sf != NULL)
          $sf->render($data);
          else {
          if ($screen != NULL) {
          $screen->render($data);
          }
          }
         * 
         */
    }

    public function renderOperation($operation, $data = null) {

        //$this->setActiveOperation($operation);      

        $app = $this->sApp;
        //call function in app to execute some jobs before rendering
        $app->doOperation($operation->getName(), $data);
        $app->beforeRenderOperation($operation,$data);

        //get operation again because doOperation may change data in activeOperation
        $operation = Blyn::app()->getActiveOperation();

        $oppUI = $operation->getUIManager();
        $serviceUI = Blyn::app()->getActiveService()->getUIManager();

        $serviceUI->addRenderView('serviceTopBar', 'serviceTopBar', array('uiObj' => $serviceUI));
        $serviceUI->addRenderView('ServiceAppLayout', $this->getLayoutView(), array('uiObj' => $this));

        $this->addRenderView('serviceAppTopBar', 'serviceAppTopBar', array('uiObj' => $this));
        $this->addRenderView('operationLayout', $oppUI->getLayoutView(), array('uiObj' => $oppUI));
        
        $app->afterRenderOperation($operation,$data);

        //because some data in operation and app changed, so set activeOperation here 
        //to syncronize the changes
        //Blyn::app()->setActiveOperation($operation);
        //Blyn::app()->setActiveServiceApp($app);

        /*

          $sf = $oppUI->getCurrentScreenFlow() == NULL ? $oppUI->getStartScreenFlow() : $oppUI->getCurrentScreenFlow();
          $screen = $oppUI->getCurrentScreen() == NULL ? $oppUI->getStartScreen() : $oppUI->getCurrentScreen();

          if ($sf != NULL) {
          $sf = is_string($sf) ? $this->getScreenFlow($sf) : $sf;
          $oppUI->addRenderView('screeflowLayout', $sf->getLayoutView(), array('uiObj' => $sf));
          $sf->render($data);
          } else {
          if ($screen != NULL) {
          $screen = is_string($screen) ? $this->getScreen($screen) : $screen;
          $oppUI->addRenderView('ScreenLayout', $screen->getLayoutView(), array('uiObj' => $screen));
          $screen->render($data);
          }
          }
         * 
         */
    }

    public function getOperationMenuItems($operation, $menuName) {
        
    }

    protected function registerMenus($style = NULL) {
        
    }

    public function getUrl($data = NULL) {

        $ajaxUrl = $this->getAjaxUrl($data);

        if ($ajaxUrl != NULL)
            return $ajaxUrl;

        return $this->buildUrl($this->service->getId(), $this->sApp->getId(), NULL, $data);
    }

    public function getStartScreenFlow() {
        $appMeta = $this->sApp->getAppConfig()->getConfig('appMeta');

        if (isset($appMeta['startScreenFlow']) && $appMeta['startScreenFlow'] != NULL)
            return $this->getScreenFlow($appMeta['startScreenFlow']);

        return NULL;
    }

    public function getStartScreen() {

        $appMeta = $this->sApp->getAppConfig()->getConfig('appMeta');

        if (isset($appMeta['startScreen']) && $appMeta['startScreen'] != NULL)
            return $this->getScreen($appMeta['startScreen']);

        return $this->getScreen('BScreenServiceApp');
    }

}

?>
